package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.abroadportmanagement.AbroadPortManagementService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/foreign")
public class AbroadPortManagementController extends BaseController {
	@Resource
	private AbroadPortManagementService abroadmanagement;
	String menuUrl = "foreign/management";// 菜单地址(权限用)

	@RequestMapping(value = "/management")
	public ModelAndView foreign(Page page) {
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			String PORT_ENGLISH_NAME = pd.getString("PORT_ENGLISH_NAME"); // 判断港口英文名
			if (null != PORT_ENGLISH_NAME && !"".equals(PORT_ENGLISH_NAME)) {
				pd.put("PORT_ENGLISH_NAME", PORT_ENGLISH_NAME.trim());
			}
			String PORT_CHINESE_NAME = pd.getString("PORT_CHINESE_NAME"); // 判断港口中文名
			if (null != PORT_CHINESE_NAME && !"".equals(PORT_CHINESE_NAME)) {
				pd.put("PORT_CHINESE_NAME", PORT_CHINESE_NAME.trim());
			}
			String COUNTRY_CHINESE_NAME = pd.getString("COUNTRY_CHINESE_NAME"); // 判断国家中文名
			if (null != COUNTRY_CHINESE_NAME && !"".equals(COUNTRY_CHINESE_NAME)) {
				pd.put("COUNTRY_CHINESE_NAME", COUNTRY_CHINESE_NAME.trim());
			}
			String COUNTRY_ENGLISH_NAME = pd.getString("COUNTRY_ENGLISH_NAME"); // 判断国家英文名
			if (null != COUNTRY_ENGLISH_NAME && !"".equals(COUNTRY_ENGLISH_NAME)) {
				pd.put("COUNTRY_ENGLISH_NAME", COUNTRY_ENGLISH_NAME.trim());
			}
			page.setPd(pd);
			List<PageData> list = abroadmanagement.seach(page);
			mv.addObject("list", list);
			mv.addObject("pd", pd);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		mv.setViewName("/business/common/abroadportmanagement/abroadportmanagement");
		return mv;

	}
	
	

    //点添加新增一条
@RequestMapping(value="/addabroad")
@ResponseBody
public String addship(){
	PageData pd = new PageData();
	    pd = this.getPageData();
		try {
			if(pd.getString("ID")==null || pd.getString("ID")==""){
				abroadmanagement.addabroad(pd);				
		}else{
			abroadmanagement.updateabroad(pd);}
		
		} catch (Exception e) {				
		
			e.printStackTrace();
		}
		return null;

}
//修改页面
@RequestMapping(value="/editabroad")
public ModelAndView shipedit(){
ModelAndView mv = this.getModelAndView();
 try {
	  PageData pd = new PageData();
	  pd = this.getPageData();
	  pd= abroadmanagement.seacById(pd);
	  mv.addObject("pd", pd);
	  mv.setViewName("/business/common/abroadportmanagement/abroadportmanagement_edit");
} catch (Exception e) {
	e.printStackTrace();
}
 return mv;
}
//删除
@RequestMapping(value="deleteabroad")
@ResponseBody
public String deleteU(){
	PageData pd = new PageData();
	pd = this.getPageData();
	try {
		abroadmanagement.delete(pd);
		
	} catch (Exception e) {
		
		e.printStackTrace();
	}
	return null;
		
}

//批量删除
@RequestMapping(value="/deleteInfo")
@ResponseBody
public String deleteinfo(){
	PageData pd = new PageData();
	pd = this.getPageData();
		try {
			
			String data = pd.getString("dataJsonStr");

			JSONArray items = JSONArray.fromObject(data);
			for(int i=0,size=items.size();i<size;i++){
				JSONObject item = items.getJSONObject(i);
				String id = item.getString("ID").trim();
				PageData pdq = new PageData();
				pdq.put("ID", id);
				abroadmanagement.delete(pdq);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	
	return null;
	

	
}
}
